package com.easyattendance.servlets;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	
	
	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
	
		  String path = request.getParameter("path");
		 
		  File f = new File (path);
		  String filename=f.getName();
		 
		  response.setContentType ("application/text"); 
		  response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");

		  InputStream in = new FileInputStream(f);
		  ServletOutputStream outs = response.getOutputStream();
		  
		  int bit = 256;
		  int i = 0;
		  try {
		  while ((bit) >= 0) {
			  bit = in.read();
			  outs.write(bit);
		  }
		  } catch (IOException ioe) {
			  ioe.printStackTrace(System.out);
		  }
		  
		  outs.flush();
		  outs.close();
		  in.close();
		

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//processRequest(request, response);
   	
	}
}
